package com.estasenposse.tdam.services;

import java.util.concurrent.ExecutionException;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RemoteViews;

import com.estasenposse.tdam.bd.DatabaseHandler;
import com.estasenposse.tdam.entities.Noticia;
import com.estasenposse.tdam.ui.Main;
import com.estasenposse.tdam.ui.NewActivity;
import com.estasenposse.tdam.ui.adapter.ImageDownloader;
import com.example.estasenposse.R;

public class WidgetProvider extends AppWidgetProvider {
	private static final String ACTION_CLICK = "ACTION_CLICK";
	private DatabaseHandler mHelper;

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		Log.i("WatchWidget", "onUpdate");
		
		for (int wid : appWidgetIds) {
			if (wid == R.id.imageButton_widget_update) {
				Log.v(WidgetProvider.class.getName(), "R.id.update");
			} else if (wid == R.id.image_widget_ppal) {
				Log.v(WidgetProvider.class.getName(),
						"R.id.image_widget_ppal");
			}
		}
		// Get all ids
		ComponentName thisWidget = new ComponentName(context,
				WidgetProvider.class);

		int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

		mHelper = new DatabaseHandler(context);
		if (mHelper.getNoteRowCount() > 0) {
			Noticia n = mHelper.getLastNote();

			RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
					R.layout.widget_layout);

			// Set the text and the image
			remoteViews.setTextViewText(R.id.update, n.getTitle());
			String url = n.getImage();

			ImageView iv = new ImageView(context);
			ProgressBar pb = new ProgressBar(context);

			pb.setId(R.id.pb_widget_image_ppal);
			iv.setId(R.id.image_widget_ppal);
			remoteViews.setViewVisibility(R.id.pb_widget_image_ppal, View.VISIBLE);

			remoteViews.setProgressBar(pb.getId(), 100, 100, false);

			ImageDownloader downloader = new ImageDownloader(iv, pb, context);
			downloader.execute(url);

			try {
				Class.forName("android.os.AsyncTask"); 
				remoteViews.setImageViewBitmap(R.id.image_widget_ppal,
						downloader.get());
			} catch (InterruptedException e) {

				e.printStackTrace();
			} catch (ExecutionException e) {

				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			remoteViews.setViewVisibility(R.id.pb_widget_image_ppal, View.INVISIBLE);

			// Register an onClickListener

			if (n.getID() != null) {
				Intent intent = new Intent(context, NewActivity.class);
				intent.putExtra("ID", Integer.parseInt(n.getID()));
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				PendingIntent pendingIntent = PendingIntent.getActivity(
						context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
				remoteViews.setOnClickPendingIntent(R.id.layout_widget,
						pendingIntent);
			} else {
				Intent intent = new Intent(context, Main.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				PendingIntent pendingIntent = PendingIntent.getActivity(
						context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
				remoteViews.setOnClickPendingIntent(R.id.layout_widget,
						pendingIntent);
			}

			Intent updateImage = new Intent(context, WidgetProvider.class);
			updateImage.setAction(ACTION_CLICK);

			PendingIntent update = PendingIntent.getBroadcast(context, 0,
					updateImage, PendingIntent.FLAG_UPDATE_CURRENT);

			remoteViews.setOnClickPendingIntent(R.id.imageButton_widget_update,
					update);

			appWidgetManager.updateAppWidget(thisWidget, remoteViews);
		}
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.v(WidgetProvider.class.getName(), "on receive");
		if (intent.getAction().equals(ACTION_CLICK)) {
			AppWidgetManager manager = AppWidgetManager.getInstance(context);

			if (mHelper == null) {
				mHelper = new DatabaseHandler(context);
			}
			int[] asd = { 1 };
			onUpdate(context, manager, asd);
			Log.v(WidgetProvider.class.getName(), "WidgetEstasEnPosse");
		} else {
			super.onReceive(context, intent);
		}

	}

}
